﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineCinemaBooking.BookingService;
using CinemaLib;

namespace OnlineCinemaBooking
{
    public partial class BookNow : System.Web.UI.Page
    {
        Movie movie;
        DateTime date;
        List<Show> shows;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                movie = (Movie)Session["MovieData"];
                date = (DateTime)Session["TimeStamp"];
                PopulateTable();
            }
            catch (NullReferenceException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (InvalidCastException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }

        private void PopulateTable()
        {
            TableTimes.Rows.Clear();
            TableRow tr = new TableRow();
            tr.BorderStyle = BorderStyle.None;
            tr.BorderWidth = 2;
            TableTimes.Rows.Add(tr);
            try
            {
                for (int i = 0; i < 7; i++)
                {
                    TableCell tc = new TableCell();
                    tc.BorderStyle = BorderStyle.None;
                    tc.BorderWidth = 2;
                    tc.Width = 75;
                    tc.HorizontalAlign = HorizontalAlign.Center;
                    tc.Text = date.AddDays(i).Date.ToString("ddd d/MM", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"));
                    TableTimes.Rows[0].Cells.Add(tc);
                }
                using (BookingService.BookingServiceClient proxy = new BookingServiceClient())
                {
                    proxy.Open();
                    shows = proxy.GetShowWithMovie(movie, DateTime.Now, DateTime.Now.AddDays(7));
                    for (int i = 0; i < 7; i++)
                    {
                        for (int j = TableTimes.Rows.Count - 1; j < shows.FindAll(sh => sh.StartTime.Date == date.AddDays(i).Date).Count; j++)
                        {
                            TableRow t = new TableRow();
                            t.BorderStyle = BorderStyle.Solid;
                            t.BorderWidth = 1;
                            for (int k = 0; k < 7; k++)
                            {
                                TableCell tc = new TableCell();
                                tc.BorderStyle = BorderStyle.None;
                                tc.BorderWidth = 2;
                                tc.Width = 75;
                                tc.HorizontalAlign = HorizontalAlign.Center;
                                t.Cells.Add(tc);
                            }
                            TableTimes.Rows.Add(t);
                        }
                        int count = 1;
                        foreach (Show s in shows.FindAll(sh => sh.StartTime.Date == date.AddDays(i).Date))
                        {
                            Button b = new Button();
                            b.Text = s.StartTime.TimeOfDay.ToString("hh':'mm") + "     ►";
                            b.BorderStyle = BorderStyle.None;
                            b.Width = 75;
                            b.Height = 23;
                            b.Font.Bold = true;
                            b.ForeColor = System.Drawing.Color.White;
                            b.CssClass = "ButtonClass";
                            b.Click += new EventHandler(TimeButton_Click);
                            b.ID = s.ID.ToString();

                            if (s.HallRoom.CountAvaible() == 0)
                            {
                                b.BackColor = System.Drawing.Color.Red;
                            }
                            else if (s.HallRoom.CountAvaible() < s.HallRoom.CountTotal() / 3)
                            {
                                b.BackColor = System.Drawing.Color.Yellow;
                            }
                            else
                            {
                                b.BackColor = System.Drawing.Color.LimeGreen;
                            }
                            TableTimes.Rows[count].Cells[i].Controls.Add(b);
                            count++;
                        }
                    }
                }
            }
            catch (HttpException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (InvalidCastException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (TimeoutException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (NullReferenceException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }

        protected void TimeButton_Click(object sender, EventArgs e)
        {
            Session["ShowData"] = shows.First(s => s.ID.ToString() == (sender as Button).ID);
            Response.Redirect("SeatSelection.aspx");
        }

        protected void LinkButtonPrev_Click(object sender, EventArgs e)
        {
            date = (DateTime)Session["TimeStamp"];
            date = date.AddDays(-7);
            Session["TimeStamp"] = date;
            PopulateTable();
        }

        protected void LinkButtonNext_Click(object sender, EventArgs e)
        {
            date = (DateTime)Session["TimeStamp"];
            date = date.AddDays(7);
            Session["TimeStamp"] = date;
            PopulateTable();
        }
    }
}